// state
// actions
// getters


// 1 引入 定义 state 函数

let list = [
  {
    id: 1,
    name: '吃饭',
    done: false,
  },
  {
    id: 2,
    name: '睡觉',
    done: true,
  },
  {
    id: 3,
    name: '打王者',
    done: false,
  },
]

import { defineStore } from 'pinia'

export const useTodoStore = defineStore('todo', {
  state: () => {
    return {


      list: JSON.parse(window.localStorage.getItem('todo')) || list,


      filters: ['All', 'Active', 'Completed'],
      active: 'All',
    }
  },
  actions: {
    // 删除功能
    delItem (id) {
      // alert(id)
      //  1通过id  找到 id  相等的那一项 进行删除 
      this.list = this.list.filter((item) => item.id !== id)

    },
    //添加数据 
    addItem (data) {
      let ids = this.list[this.list.length - 1].id
      ids++


      let id = ids

      //   console.log(++this.list[this.list.length - 1].id)
      this.list.unshift({
        id: id,
        name: data,
        done: false,

      })

    },
    //  单选 
    changeChecked (status) {
      this.list.forEach((item) => item.done = status)



    },
    // 清除已经完成 
    clearDoneClick () {
      this.list = this.list.filter((item) => !item.done)
    },



    currentActive (data) {
      this.active = data // 高亮的同时 切换数据
      // this.active 是不是变化 

    }


  },
  getters: {

    //  全选的状态
    getFlag () {
      return this.list.every((item) => item.done)
    },


    //  没有被完成的
    leftCount () {
      return this.list.filter((item) => !item.done).length

    },

    showList () {


      if (this.active == 'Completed') {
        return this.list.filter((item) => item.done)
      } else if (this.active == 'Active') {
        return this.list.filter((item) => !item.done)
      } else {
        return this.list // all 
      }
    },




  }

})






// export const userNameStore = defineStore('names', {
//   state: () => {
//     return {

//     }
//   },
//   actions: {},
//   getters: {}
// })
